<?php

namespace App\Admin\Controllers;

use App\Admin\Js;
use App\Models\Activity;
use App\Http\Controllers\Controller;
use App\Models\Product;
use App\Services\SecKillService;
use Carbon\Carbon;
use Encore\Admin\Admin;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Http\Request;

class ActivityController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('活动列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('详情')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @param Product $product
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('创建活动')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        Admin::script(Js::ajaxConfirm('/admin/activity/audit', array('id')));
        $grid = new Grid(new Activity);
        $grid->id('Id');
        $grid->column('product.title', '商品名称');
        $grid->type('类型')->display(function ($value) {
            return $value == 1 ? '限量' : '限时';
        });
        $grid->quantity('数量');
        $grid->start_time('开始时间');
        $grid->end_time('结束时间');
        $grid->status('审核状态')->display(function ($value) {
            return $value ? '已审核' : '未审核';
        });
        $grid->created_at('创建时间');

        $grid->actions(function ($actions) {
            if ($actions->row->status) {
                $actions->disableDelete();
                $actions->disableEdit();
            } else {
                $id = $actions->getKey();
                $actions->append("<a href=\"javascript:void(0);\" class=\"ajax-confirm\" data-id=\"{$id}\"><i class=\"fa fa-check-square\"></i></a>");
            }
        });


        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $activity = Activity::findOrFail($id);
        $show = new Show($activity);
        $product = $activity->product;
        $show->all();
        $show->product_id()->as(function ($product_id) use ($product) {
            return $product->title;
        });
        if ($activity->status) {
            $show->panel()
                ->tools(function ($tools) {
                    $tools->disableEdit();
                    $tools->disableDelete();
                });;
        }

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @param Product|null $product
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Activity);

        $form->text('title', '活动标题')->rules(['required']);
        $form->select('product_id', '商品')->options(function ($id) {
            $product = Product::find($id);
            if ($product) {
                return [$product->id => $product->title];
            }
        })->ajax('/admin/api/products');
        $form->select('type')->options([1 => '限量', 2 => '限时']);
        $form->number('quantity', '数量')->rules(['min: 0']);
        $form->datetime('start_time', '开始时间')->rules(['required']);
        $form->datetime('end_time', '结束时间')->rules(['required']);

        return $form;
    }

    //审核
    public function audit(Request $request, SecKillService $service)
    {
        $id = (int)$request->input('id');
        $activity = Activity::findOrFail($id);
        $activity->status = true;
        $result = $activity->save();
        if ($result) {
            try {
                $service->addPlan($activity);
            }catch (\Exception $e) {
                return response()->json(['msg'=> $e->getMessage()]);
            }
            return response()->json(['msg'=> 'success']);
        }
        return response()->json(['msg'=>'error']);
    }
}
